/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * ConsultaClientes.java
 *
 * Created on 04/03/2010, 11:06:52
 */

package documenteditor.view;

import documenteditor.DocumentEditorView;
import documenteditor.controller.ClienteJpaController;
import documenteditor.controller.ClientePessoaFisicaJpaController;
import documenteditor.entidade.model.Cliente;
import documenteditor.entidade.model.ClientePessoaFisica;
import java.util.ArrayList;
import javax.swing.JOptionPane;
import org.jdesktop.application.Action;

/**
 *
 * @author gleidson.moura
 */
public class ConsultaClientes extends javax.swing.JPanel {

    /** Creates new form ConsultaClientes */
    public ConsultaClientes() {
        initComponents();
        jButtonAnterior.setVisible(Boolean.FALSE);
        jButtonProxima.setVisible(Boolean.FALSE);
        totalResults = new Integer(0);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {
        bindingGroup = new org.jdesktop.beansbinding.BindingGroup();

        listaClientes = new ArrayList();
        jLabel1 = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        jTablePesquisarClientes = new javax.swing.JTable();
        jLabelNomeCliente = new javax.swing.JLabel();
        jTextFieldNomeCliente = new javax.swing.JTextField();
        jButtonListarClientes = new javax.swing.JButton();
        jButtonProxima = new javax.swing.JButton();
        jButtonAnterior = new javax.swing.JButton();
        jButton1 = new javax.swing.JButton();
        jPanel1 = new javax.swing.JPanel();
        jLabel2 = new javax.swing.JLabel();
        jScrollPane2 = new javax.swing.JScrollPane();
        jTablePesquisarClientes1 = new javax.swing.JTable();
        jLabelNomeCliente1 = new javax.swing.JLabel();
        jTextFieldNomeCliente1 = new javax.swing.JTextField();
        jButtonListarClientes1 = new javax.swing.JButton();
        jButtonProxima1 = new javax.swing.JButton();
        jButtonAnterior1 = new javax.swing.JButton();
        jButton2 = new javax.swing.JButton();

        setName("Form"); // NOI18N
        setRequestFocusEnabled(false);

        org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance(documenteditor.DocumentEditorApp.class).getContext().getResourceMap(ConsultaClientes.class);
        jLabel1.setFont(resourceMap.getFont("jLabel1.font")); // NOI18N
        jLabel1.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        jLabel1.setText(resourceMap.getString("jLabel1.text")); // NOI18N
        jLabel1.setName("jLabel1"); // NOI18N

        jScrollPane1.setName("jScrollPane1"); // NOI18N

        jTablePesquisarClientes.setAutoscrolls(false);
        jTablePesquisarClientes.setName("jTablePesquisarClientes"); // NOI18N
        jTablePesquisarClientes.setRequestFocusEnabled(false);

        org.jdesktop.swingbinding.JTableBinding jTableBinding = org.jdesktop.swingbinding.SwingBindings.createJTableBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, listaClientes, jTablePesquisarClientes);
        org.jdesktop.swingbinding.JTableBinding.ColumnBinding columnBinding = jTableBinding.addColumnBinding(org.jdesktop.beansbinding.ELProperty.create("${idCliente}"));
        columnBinding.setColumnName("Id Cliente");
        columnBinding.setColumnClass(Long.class);
        columnBinding = jTableBinding.addColumnBinding(org.jdesktop.beansbinding.ELProperty.create("${nomeCliente}"));
        columnBinding.setColumnName("Nome Cliente");
        columnBinding.setColumnClass(String.class);
        columnBinding = jTableBinding.addColumnBinding(org.jdesktop.beansbinding.ELProperty.create("${email}"));
        columnBinding.setColumnName("Email");
        columnBinding.setColumnClass(String.class);
        columnBinding = jTableBinding.addColumnBinding(org.jdesktop.beansbinding.ELProperty.create("${dataCadastro}"));
        columnBinding.setColumnName("Data Cadastro");
        columnBinding.setColumnClass(java.util.Date.class);
        columnBinding = jTableBinding.addColumnBinding(org.jdesktop.beansbinding.ELProperty.create("${qtdPontos}"));
        columnBinding.setColumnName("Qtd Pontos");
        columnBinding.setColumnClass(Long.class);
        bindingGroup.addBinding(jTableBinding);
        jTableBinding.bind();
        jTablePesquisarClientes.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jTablePesquisarClientesMouseClicked(evt);
            }
        });
        jScrollPane1.setViewportView(jTablePesquisarClientes);

        jLabelNomeCliente.setText(resourceMap.getString("jLabelNomeCliente.text")); // NOI18N
        jLabelNomeCliente.setName("jLabelNomeCliente"); // NOI18N

        jTextFieldNomeCliente.setText(resourceMap.getString("jTextFieldNomeCliente.text")); // NOI18N
        jTextFieldNomeCliente.setName("jTextFieldNomeCliente"); // NOI18N
        jTextFieldNomeCliente.setNextFocusableComponent(jButtonListarClientes);

        jButtonListarClientes.setText(resourceMap.getString("jButtonListarClientes.text")); // NOI18N
        jButtonListarClientes.setToolTipText(resourceMap.getString("jButtonListarClientes.toolTipText")); // NOI18N
        jButtonListarClientes.setName("jButtonListarClientes"); // NOI18N
        jButtonListarClientes.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                listarClientes(evt);
            }
        });

        javax.swing.ActionMap actionMap = org.jdesktop.application.Application.getInstance(documenteditor.DocumentEditorApp.class).getContext().getActionMap(ConsultaClientes.class, this);
        jButtonProxima.setAction(actionMap.get("proximaConsulta")); // NOI18N
        jButtonProxima.setIcon(resourceMap.getIcon("jButtonProxima.icon")); // NOI18N
        jButtonProxima.setText(resourceMap.getString("jButtonProxima.text")); // NOI18N
        jButtonProxima.setActionCommand(resourceMap.getString("jButtonProxima.actionCommand")); // NOI18N
        jButtonProxima.setName("jButtonProxima"); // NOI18N

        jButtonAnterior.setAction(actionMap.get("consultaAnterior")); // NOI18N
        jButtonAnterior.setIcon(resourceMap.getIcon("jButtonAnterior.icon")); // NOI18N
        jButtonAnterior.setText(resourceMap.getString("jButtonAnterior.text")); // NOI18N
        jButtonAnterior.setName("jButtonAnterior"); // NOI18N

        jButton1.setText(resourceMap.getString("jButton1.text")); // NOI18N
        jButton1.setName("jButton1"); // NOI18N

        jPanel1.setName("jPanel1"); // NOI18N
        jPanel1.setRequestFocusEnabled(false);

        jLabel2.setFont(resourceMap.getFont("jLabel2.font")); // NOI18N
        jLabel2.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        jLabel2.setText(resourceMap.getString("jLabel2.text")); // NOI18N
        jLabel2.setName("jLabel2"); // NOI18N

        jScrollPane2.setName("jScrollPane2"); // NOI18N

        jTablePesquisarClientes1.setAutoscrolls(false);
        jTablePesquisarClientes1.setName("jTablePesquisarClientes1"); // NOI18N
        jTablePesquisarClientes1.setRequestFocusEnabled(false);
        jTablePesquisarClientes1.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jTablePesquisarClientes1MouseClicked(evt);
            }
        });
        jScrollPane2.setViewportView(jTablePesquisarClientes1);

        jLabelNomeCliente1.setText(resourceMap.getString("jLabelNomeCliente1.text")); // NOI18N
        jLabelNomeCliente1.setName("jLabelNomeCliente1"); // NOI18N

        jTextFieldNomeCliente1.setName("jTextFieldNomeCliente1"); // NOI18N
        jTextFieldNomeCliente1.setNextFocusableComponent(jButtonListarClientes);

        jButtonListarClientes1.setText(resourceMap.getString("jButtonListarClientes1.text")); // NOI18N
        jButtonListarClientes1.setToolTipText(resourceMap.getString("jButtonListarClientes1.toolTipText")); // NOI18N
        jButtonListarClientes1.setName("jButtonListarClientes1"); // NOI18N
        jButtonListarClientes1.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jButtonListarClientes1listarClientes(evt);
            }
        });

        jButtonProxima1.setAction(actionMap.get("proximaConsulta")); // NOI18N
        jButtonProxima1.setIcon(null);
        jButtonProxima1.setActionCommand(resourceMap.getString("jButtonProxima1.actionCommand")); // NOI18N
        jButtonProxima1.setName("jButtonProxima1"); // NOI18N

        jButtonAnterior1.setAction(actionMap.get("consultaAnterior")); // NOI18N
        jButtonAnterior1.setIcon(null);
        jButtonAnterior1.setName("jButtonAnterior1"); // NOI18N

        jButton2.setText(resourceMap.getString("jButton2.text")); // NOI18N
        jButton2.setName("jButton2"); // NOI18N

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jLabel2, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 803, Short.MAX_VALUE)
                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, jPanel1Layout.createSequentialGroup()
                        .addComponent(jLabelNomeCliente1)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jTextFieldNomeCliente1, javax.swing.GroupLayout.PREFERRED_SIZE, 374, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(jButtonListarClientes1)
                        .addGap(18, 18, 18)
                        .addComponent(jButton2)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 142, Short.MAX_VALUE)
                        .addComponent(jButtonAnterior1, javax.swing.GroupLayout.PREFERRED_SIZE, 33, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jButtonProxima1, javax.swing.GroupLayout.PREFERRED_SIZE, 33, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addComponent(jScrollPane2, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 803, Short.MAX_VALUE))
                .addContainerGap())
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addComponent(jLabel2, javax.swing.GroupLayout.PREFERRED_SIZE, 25, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(jLabelNomeCliente1)
                        .addComponent(jTextFieldNomeCliente1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(jButtonListarClientes1)
                        .addComponent(jButton2))
                    .addComponent(jButtonProxima1)
                    .addComponent(jButtonAnterior1))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 417, Short.MAX_VALUE)
                .addContainerGap())
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jLabel1, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 803, Short.MAX_VALUE)
                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                        .addComponent(jLabelNomeCliente)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jTextFieldNomeCliente, javax.swing.GroupLayout.PREFERRED_SIZE, 374, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(jButtonListarClientes)
                        .addGap(18, 18, 18)
                        .addComponent(jButton1)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 142, Short.MAX_VALUE)
                        .addComponent(jButtonAnterior, javax.swing.GroupLayout.PREFERRED_SIZE, 33, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jButtonProxima, javax.swing.GroupLayout.PREFERRED_SIZE, 33, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 803, Short.MAX_VALUE))
                .addContainerGap())
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup()
                    .addGap(0, 0, Short.MAX_VALUE)
                    .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGap(0, 0, Short.MAX_VALUE)))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 25, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(jLabelNomeCliente)
                        .addComponent(jTextFieldNomeCliente, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(jButtonListarClientes)
                        .addComponent(jButton1))
                    .addComponent(jButtonProxima)
                    .addComponent(jButtonAnterior))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 417, Short.MAX_VALUE)
                .addContainerGap())
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup()
                    .addGap(0, 0, Short.MAX_VALUE)
                    .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGap(0, 0, Short.MAX_VALUE)))
        );

        bindingGroup.bind();
    }// </editor-fold>//GEN-END:initComponents

    private void listarClientes(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_listarClientes
        listarClientes();
    }//GEN-LAST:event_listarClientes

    private void jTablePesquisarClientesMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jTablePesquisarClientesMouseClicked
        if (evt.getClickCount() >= 1) {
            int row = jTablePesquisarClientes.getSelectedRow();
            Object codigoCliente = jTablePesquisarClientes.getValueAt(row,0);
            ClientePessoaFisicaJpaController controllerCli = new ClientePessoaFisicaJpaController();
            ClientePessoaFisica cliente = controllerCli.findClientePessoaFisica(new Long(codigoCliente.toString()));
            DocumentEditorView.scrollPane.setViewportView(new CadastroCliente(cliente));
        }
    }//GEN-LAST:event_jTablePesquisarClientesMouseClicked

    private void jTablePesquisarClientes1MouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jTablePesquisarClientes1MouseClicked
        // TODO add your handling code here:
    }//GEN-LAST:event_jTablePesquisarClientes1MouseClicked

    private void jButtonListarClientes1listarClientes(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jButtonListarClientes1listarClientes
        // TODO add your handling code here:
    }//GEN-LAST:event_jButtonListarClientes1listarClientes

    private void listarClientes() {
        nomeCliente = jTextFieldNomeCliente.getText();
        contResults = new Integer(0);
        if (nomeCliente != null && !nomeCliente.equals("")) {

            ClienteJpaController controller = new ClienteJpaController();
            totalResults = controller.getClientePessoaFisicaCount(nomeCliente+"%");


            jButtonAnterior.setVisible(totalResults > 40);
            jButtonAnterior.setEnabled(Boolean.FALSE);
            jButtonProxima.setVisible(totalResults > 40);

            listaClientes = controller.findClienteEntities(40, contResults, nomeCliente+"%");

            if (listaClientes.size() == 0) {
                JOptionPane.showMessageDialog(this,"Nenhum cliente encontrado para o filtro informado!", "Consulta de Clientes", JOptionPane.INFORMATION_MESSAGE);

            }
            montarTabela();
        } else {
            JOptionPane.showMessageDialog(this,"Digite o nome do cliente!", "Consulta de Clientes", JOptionPane.ERROR_MESSAGE);
        }
    }

    @Action
    public void proximaConsulta() {
        if (!jTextFieldNomeCliente.getText().equals(nomeCliente)) {
            listarClientes();
            return;
        }
        ClienteJpaController controller = new ClienteJpaController();
        listaClientes = controller.findClienteEntities(40, contResults + 40, nomeCliente+"%");
        montarTabela();
        jButtonAnterior.setEnabled(Boolean.TRUE);
        contResults += 40;
        if (contResults >= totalResults - 40) {
            jButtonProxima.setEnabled(Boolean.FALSE);
        }
    }

    @Action
    public void consultaAnterior() {
        if (!jTextFieldNomeCliente.getText().equals(nomeCliente)) {
            listarClientes();
            return;
        }
        ClienteJpaController controller = new ClienteJpaController();
        listaClientes = controller.findClienteEntities(40, contResults - 40, nomeCliente+"%");
        montarTabela();
        jButtonProxima.setEnabled(Boolean.TRUE);
        contResults -= 40;
        if (contResults <  40) {
            jButtonAnterior.setEnabled(Boolean.FALSE);
        }
    }

    private void montarTabela() {
        org.jdesktop.swingbinding.JTableBinding jTableBinding = org.jdesktop.swingbinding.SwingBindings.createJTableBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, listaClientes, jTablePesquisarClientes);
        org.jdesktop.swingbinding.JTableBinding.ColumnBinding columnBinding = jTableBinding.addColumnBinding(org.jdesktop.beansbinding.ELProperty.create("${idCliente}"));
        columnBinding.setColumnName("Id Cliente");
        columnBinding.setColumnClass(Long.class);
        columnBinding = jTableBinding.addColumnBinding(org.jdesktop.beansbinding.ELProperty.create("${nomeCliente}"));
        columnBinding.setColumnName("Nome Cliente");
        columnBinding.setColumnClass(String.class);
        columnBinding = jTableBinding.addColumnBinding(org.jdesktop.beansbinding.ELProperty.create("${email}"));
        columnBinding.setColumnName("Email");
        columnBinding.setColumnClass(String.class);
        columnBinding = jTableBinding.addColumnBinding(org.jdesktop.beansbinding.ELProperty.create("${dataCadastro}"));
        columnBinding.setColumnName("Data Cadastro");
        columnBinding.setColumnClass(java.util.Date.class);
        columnBinding = jTableBinding.addColumnBinding(org.jdesktop.beansbinding.ELProperty.create("${qtdPontos}"));
        columnBinding.setColumnName("Qtd Pontos");
        columnBinding.setColumnClass(Long.class);
        bindingGroup.addBinding(jTableBinding);
        jTableBinding.bind();
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JButton jButtonAnterior;
    private javax.swing.JButton jButtonAnterior1;
    private javax.swing.JButton jButtonListarClientes;
    private javax.swing.JButton jButtonListarClientes1;
    private javax.swing.JButton jButtonProxima;
    private javax.swing.JButton jButtonProxima1;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabelNomeCliente;
    private javax.swing.JLabel jLabelNomeCliente1;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JTable jTablePesquisarClientes;
    private javax.swing.JTable jTablePesquisarClientes1;
    private javax.swing.JTextField jTextFieldNomeCliente;
    private javax.swing.JTextField jTextFieldNomeCliente1;
    private java.util.List<Cliente> listaClientes;
    private org.jdesktop.beansbinding.BindingGroup bindingGroup;
    // End of variables declaration//GEN-END:variables
    private Integer contResults;
    private Integer totalResults;
    private String nomeCliente;
}
